//
//  JwyZhuanfaController.swift
//  GZWeibo007
//
//  Created by Mac on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class JwyZhuanfaController: UIViewController {

    var statu:CZStatus?//微博模型
    
    var tag=0//标记
    
    init(tag:Int) {
        
        self.tag=tag
        
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置item
        prepareItem()
        
        //设置文本输入框
        view.addSubview(textView)
        textView.ff_Fill(view)
        
        
    }

 //MARK:-----懒加载
    private lazy var textView:UITextView={
        
        let textView=UITextView()
        
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.font = UIFont.systemFontOfSize(18)
        textView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        textView.textColor = UIColor.blackColor()
        textView.bounces = true
        textView.alwaysBounceVertical = true
        textView.becomeFirstResponder()
        
        return textView
    }()
    
//MARK:------ 设置item
     private func prepareItem(){
        
        self.navigationItem.leftBarButtonItem=UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "quxiao")
        self.navigationItem.rightBarButtonItem=UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendMessage")
    }
    
    
    //item点击事件
    //取消
    func quxiao(){
        
        self.textView.endEditing(true)
        self.dismissViewControllerAnimated(true, completion: nil)
        
        
    }
    
    
    //发送
    func sendMessage(){
        
        let text=textView.text
        if text.characters.count>140{
            SVProgressHUD.showInfoWithStatus("靠，内容不能超过140个字")
            return
        }
        if text == nil || text == "" {
            SVProgressHUD.showInfoWithStatus("内容不能为空")
            return
        }
        
        if tag==1{//转发
            
            CZNetworkTool.sharedInstance.loadRepostData((statu?.id)!, status: text, finished: { (result, error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                SVProgressHUD.showInfoWithStatus("发送成功")
                self.quxiao()
            })
            
        }else if tag==2{//评论
            
            CZNetworkTool.sharedInstance.loadPinglunData((statu?.id)!, comment: text, finished: { (result, error) -> () in
                if error != nil {
                    
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                // 发送成功, 直接关闭界面
                
                self.quxiao()
                SVProgressHUD.showInfoWithStatus("发送成功")
            })
        }else if tag==3{//发微博
            
            
            CZNetworkTool.sharedInstance.sendWeiBo(text, finished: { (result, error) -> () in
                
                if error != nil {
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                // 发送成功, 直接关闭界面
               
                
                self.quxiao()
                 SVProgressHUD.showInfoWithStatus("发送成功")

            })
            
           
        }
        self.textView.endEditing(true)
    }
    
}




//MARK:---------自定义titleView

class JwyitemView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //准备UI
    func prepareUI(){
        
        addSubview(topLabel)
        addSubview(userNameLabel)
        
        userNameLabel.ff_AlignInner(type: ff_AlignType.TopCenter, referView: self, size: nil)
        topLabel.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: self, size: nil)
        
    }
    
//MARK:--------懒加载
    lazy var topLabel:UILabel={
        
        let lbl=UILabel()
        //lbl.textColor=UIColor.lightGrayColor()
        lbl.font=UIFont.systemFontOfSize(16)
        
        return lbl
        
    }()
    
    lazy var userNameLabel:UILabel={
        
        let lbl=UILabel()
        lbl.textColor=UIColor.lightGrayColor()
        lbl.font=UIFont.systemFontOfSize(13)
        
        return lbl
        
    }()
}